package com.zhiyou.app.common.constant;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.MD5;
import com.zhiyou.app.common.exception.BizException;
import com.zhiyou.app.common.starejob.StartJob;
import com.zhiyou.app.pojo.vo.dict.IdColorItem;
import com.zhiyou.app.pojo.vo.dict.IdSizeGroup;
import com.zhiyou.app.pojo.vo.dict.IdSizeItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static com.zhiyou.app.common.constant.CommonConstant.*;


/**
 * 初始化字典数据
 */
@Slf4j
@Component
public class DictLoaderRunner implements StartJob {
    @Resource
    private ResourceLoader resourceLoader;

    @Override
    public void run() throws Exception {
        List<IdSizeGroup> idSizeGroups = new ArrayList<>();
        List<IdColorItem> idColorItems = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceLoader.getResource("classpath:/dict/size_list.csv").getInputStream()))) {
            String line;
            String groupName = null;
            int i = 0;
            while ((line = reader.readLine()) != null) {
                //跳过第一行
                if(i++ == 0){
                    continue;
                }
                String[] parts = line.split(",");
                IdSizeGroup group = getGroup(parts, groupName, idSizeGroups);
                groupName = group.getGroupName();
                IdSizeItem idSizeItem = new IdSizeItem()
                        .setDisplayName(parts[1] + "\t\t(" + parts[2] + ", " + parts[3] + ")")
                        .setName(parts[1]).setWidth(Integer.parseInt(parts[2]))
                        .setHeight(Integer.parseInt(parts[3])).setColor(parts[4].equals("/") ? null : parts[4]);
                String key = DigestUtil.md5Hex(idSizeItem.getDisplayName());
                group.getIdSizeItems().add(idSizeItem.setKey(key));
                DICT_POOL.put(key, idSizeItem);
            }
            ID_SIZE_GROUP_LIST.addAll(idSizeGroups);
        } catch (IOException e) {
            throw new BizException("读取字典文件失败", e.getLocalizedMessage());
        }
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceLoader.getResource("classpath:/dict/color_list.csv").getInputStream()))) {
            String line;
            int j = 0;
            while ((line = reader.readLine()) != null) {
                //跳过第一行
                if(j++ == 0){
                    continue;
                }
                String[] parts = line.split(",");
                IdColorItem idColorItem = new IdColorItem().setKey(parts[0]).setDisplayName(parts[1]).setRgb(parts[2]);
                idColorItems.add(idColorItem);
                DICT_POOL.put(idColorItem.getKey(), idColorItem);
            }
            ID_COLOR_LIST.addAll(idColorItems);
        } catch (IOException e) {
            throw new BizException("读取字典文件失败", e.getLocalizedMessage());
        }
    }

    private static IdSizeGroup getGroup(String[] parts, String groupName, List<IdSizeGroup> idSizeGroups) {
        IdSizeGroup idSizeGroup;
        if (parts[0].equals(groupName)) {
            idSizeGroup = idSizeGroups.get(idSizeGroups.size() - 1);
        } else {
            idSizeGroup = new IdSizeGroup().setGroupName(parts[0]).setIdSizeItems(new ArrayList<>());
            idSizeGroups.add(idSizeGroup);
        }
        return idSizeGroup;
    }
}
